package com.martin.dms.model.hib;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import com.martin.dms.model.Category;
import com.martin.dms.model.Item;

/**
 * Hibernate category implementation.
 * 
 * @author mtsvetkov
 */
public class CategoryHib implements Category {

    private Long id;

    private String name;

    private Set<Item> items = new HashSet<Item>();

    /**
     * Default constructor.
     */
    public CategoryHib() {
    }

    /**
     * {@inheritDoc}
     */
    public Long getId() {
        return id;
    }

    /**
     * {@inheritDoc}
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the name of the catecory.
     * 
     * @param name - the name of the category.
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * {@inheritDoc}
     */
    public Set<Item> getItems() {
        return Collections.unmodifiableSet(items);
    }

    /**
     * Sets the items for this category.
     * 
     * @param items to set.
     */
    public void setItems(Set<Item> items) {
        this.items = items;
    }

    /**
     * Adds an item to this category.
     * 
     * @param item to add.
     */
    public void addItem(ItemHib item) {
        items.add(item);
        item.setCategory(this);
    }

    /**
     * Removes an item from this category.
     * 
     * @param item to remove.
     */
    public void removeItem(ItemHib item) {
        items.remove(item);
        item.setCategory(null);
    }

    /**
     * {@inheritDoc}
     */
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof CategoryHib)) {
            return false;
        }
        final CategoryHib category = (CategoryHib) obj;
        if (category.getId() != null && this.getId() != null) {
            return category.getId().equals(this.getId());
        }
        return false;
    }

    /**
     * {@inheritDoc}
     */
    public int hashCode() {
        int result = 17;
        if (getId() != null) {
            result += 37 * result + getId().hashCode();
        }
        return result;
    }
}
